package com.redxiii.spike.pmi.command;

import com.redxiii.spike.api.command.CommandFactory;
import com.redxiii.spike.api.command.InitializationRequired;
import com.redxiii.spike.api.communication.CommunicationService.DefaultMsgs;
import com.redxiii.spike.api.message.ImMessage;
import com.redxiii.spike.api.pmi.entity.Issue;
import com.redxiii.spike.pmi.HandlerSession;
import com.redxiii.spike.pmi.commons.UserSettings;
import com.redxiii.spike.util.command.InternetCommand;
import com.redxiii.spike.util.message.BasicMsgInterpreter;

/**
 * @author Daniel Filgueiras
 * 04/05/2011
 */
@CommandFactory(BasicMsgInterpreter.class)
@InitializationRequired
public class PostComment extends InternetCommand  {

	@Override
	public boolean proccess(ImMessage message) {
		UserSettings settings = HandlerSession.getUserSettings(remoteId);
		long issueNumber = settings.getWorkingIssue();
		
		if (settings.getWorkingIssue() == 0) {
			service.reply(message, "You are not working on any issue");
		} else {
			Issue issue = HandlerSession.getDatabase().getIssue(issueNumber);
			
			if (issue != null) {
				String text = message.toText().substring(9);
				
				settings.setIssueComment(text);
				service.reply(message, DefaultMsgs.Accepted);
			} else {
				service.reply(message, "Unknown issue");
			}
		}
		
		return true;
	}
}
